<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon32.ico?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon16.ico?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="CG,">










<meta name="description" content="Fundamentals of Computer Graphics六七章的笔记，是比较重要的基础知识。其中主要包括了affine和homography两种变换。">
<meta name="keywords" content="CG">
<meta property="og:type" content="article">
<meta property="og:title" content="Viewing matrices">
<meta property="og:url" content="http://htelepathh.gitee.io/2019/08/14/Viewing-matrices/index.html">
<meta property="og:site_name" content="OUTPUT">
<meta property="og:description" content="Fundamentals of Computer Graphics六七章的笔记，是比较重要的基础知识。其中主要包括了affine和homography两种变换。">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://htelepathh.gitee.io/2019/08/14/Viewing-matrices/camera_tr.jpg">
<meta property="og:image" content="http://htelepathh.gitee.io/2019/08/14/Viewing-matrices/perspec_tr.jpg">
<meta property="og:updated_time" content="2019-09-16T08:58:48.990Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Viewing matrices">
<meta name="twitter:description" content="Fundamentals of Computer Graphics六七章的笔记，是比较重要的基础知识。其中主要包括了affine和homography两种变换。">
<meta name="twitter:image" content="http://htelepathh.gitee.io/2019/08/14/Viewing-matrices/camera_tr.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":true},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://htelepathh.gitee.io/2019/08/14/Viewing-matrices/">





  <title>Viewing matrices | OUTPUT</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">OUTPUT</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">HTelepathH's blog</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-message">
          <a href="/message" rel="section">
            
            留言
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://htelepathh.gitee.io/2019/08/14/Viewing-matrices/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="HTelepathH">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/MOB.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="OUTPUT">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Viewing matrices</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-08-14T22:23:29+08:00">
                2019-08-14
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/math/" itemprop="url" rel="index">
                    <span itemprop="name">math</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p><em>Fundamentals of Computer Graphics</em>六七章的笔记，是比较重要的基础知识。其中主要包括了affine和homography两种变换。<br><a id="more"></a></p>
<h2 id="Affine-Transformation"><a href="#Affine-Transformation" class="headerlink" title="Affine Transformation"></a>Affine Transformation</h2><p>仿射变换即线性变换(scaling, shearing, rotation, reflection, translations)，和坐标维度的线性变换，高维恒为1为了将translations和其他变化统一起来。每个维度有4个参数控制</p>
<script type="math/tex; mode=display">
\left[
 \begin{matrix}
   x'\\
   y'\\
   z'\\
   1\\
  \end{matrix} 
\right]=
\left[
 \begin{matrix}
   m_{11} & m_{12} & m_{13} & x_t\\
   m_{21} & m_{22} & m_{23} & y_t\\
   m_{31} & m_{32} & m_{33} & z_t\\
   0 & 0 & 0 & 1
  \end{matrix} 
\right]
\left[
 \begin{matrix}
   x\\
   y\\
   z\\
   1\\
  \end{matrix} 
\right]</script><h3 id="Viewport-Transformation-windowing"><a href="#Viewport-Transformation-windowing" class="headerlink" title="Viewport Transformation(windowing)"></a>Viewport Transformation(windowing)</h3><p>将canonical空间坐标$[-1,1]^3$的映射到画面的像素上，变换只包含了translation和scaling。z坐标在windowing中无关，需要在之后的z-buffer内排序。</p>
<script type="math/tex; mode=display">
\left[
 \begin{matrix}
   x_{pixel}\\
   y_{pixel}\\
   z_{canonical}\\
   1\\
  \end{matrix} 
\right]=
\left[
 \begin{matrix}
   \frac{n_x}{2} & 0 & 0 &\frac{n_x-1}{2}\\
   0 & \frac{n_y}{2} & 0 &\frac{n_y-1}{2}\\
   0 & 0 & 1 & 0\\
   0 & 0 & 0 & 1
  \end{matrix} 
\right]
\left[
 \begin{matrix}
   x_{canonical}\\
   y_{canonical}\\
   z_{canonical}\\
   1\\
  \end{matrix} 
\right]</script><h3 id="The-Orthographic-Projection-Transformation"><a href="#The-Orthographic-Projection-Transformation" class="headerlink" title="The Orthographic Projection Transformation"></a>The Orthographic Projection Transformation</h3><p>将三维空间坐标正投影到$[-1,1]^3$的canonical空间，就是使用六个平面(right, left, top, bottom, near, far)确定view volume，然后正投影到canonical,同样只包含了translation和scaling。</p>
<script type="math/tex; mode=display">
\left[
 \begin{matrix}
   x_{canonical}\\
   y_{canonical}\\
   z_{canonical}\\
   1\\
  \end{matrix} 
\right]=
\left[
 \begin{matrix}
   \frac{2}{r-l} & 0 & 0 &-\frac{r+l}{r-l}\\
   0 & \frac{2}{t-b} & 0 &-\frac{t+b}{t-b}\\
   0 & 0 & \frac{2}{n-f} & -\frac{n+f}{n-f}\\
   0 & 0 & 0 & 1
  \end{matrix} 
\right]
\left[
 \begin{matrix}
   x\\
   y\\
   z\\
   1\\
  \end{matrix} 
\right]</script><h3 id="Camera-Transformation"><a href="#Camera-Transformation" class="headerlink" title="Camera Transformation"></a>Camera Transformation</h3><p>将世界坐标$(x, y, z)$换基到相机坐标系$(u, v, w)$，相机坐标轴中，$w$与gaze direction相反，t是世界中的view up向量，$u$由t叉积<em>w</em>得到，$v$由$w$叉积$u$得到。<br><img src="/2019/08/14/Viewing-matrices/camera_tr.jpg" title="Camera Transformation (from Figure 7.7)"></p>
<script type="math/tex; mode=display">M_{cam}=
\left[
 \begin{matrix}
   u & v & w & e\\
   0 & 0 & 0 & 1
  \end{matrix} 
\right]^{-1}=
\left[
 \begin{matrix}
   x_u & y_u & z_u & 0\\
   x_v & y_v & z_v & 0\\
   x_w & y_w & z_w & 0\\
   0 & 0 & 0 & 1
  \end{matrix} 
\right]
\left[
 \begin{matrix}
   1 & 0 & 0 &-x_e\\
   0 & 1 & 0 &-y_e\\
   0 & 0 & 1 &-z_e\\
   0 & 0 & 0 & 1
  \end{matrix} 
\right]</script><h2 id="Homography-Transformation"><a href="#Homography-Transformation" class="headerlink" title="Homography Transformation"></a>Homography Transformation</h2><p>为了给变换增加更多自由度，使用了齐次坐标，定义$(x,y,z)= (x’/w’,y’/w’,z’/w’)$,由于使用比值形式，每个维度由8个参数控制。</p>
<script type="math/tex; mode=display">
\left[
 \begin{matrix}
   x'\\
   y'\\
   z'\\
   w'\\
  \end{matrix} 
\right]=
\left[
 \begin{matrix}
   a_1 & b_1 & c_1 &d_1\\
   a_2 & b_2 & c_2 &d_2\\
   a_3 & b_3 & c_3 &d_3\\
   e& f & g & h
  \end{matrix} 
\right]
\left[
 \begin{matrix}
   x\\
   y\\
   z\\
   1\\
  \end{matrix} 
\right]</script><h3 id="Perspective-Transformation"><a href="#Perspective-Transformation" class="headerlink" title="Perspective Transformation"></a>Perspective Transformation</h3><p>Perspective transformation就是一种Homography，具体表现为三角形的相似。将三维世界坐标做近大远小的变换。$n$为near平面，$f$为far平面，由图可见Homography保持near平面上的点不变，缩放了near平面以后的点(仅在x和y上)，从而使view volume变为一个正投影空间。<br><img src="/2019/08/14/Viewing-matrices/perspec_tr.jpg" title="Perspective Transformation (from Figure 7.13)"></p>
<script type="math/tex; mode=display">P=
\left[
 \begin{matrix}
   n & 0 & 0 & 0\\
   0 & n & 0 & 0\\
   0 & 0 & n+f & -fn\\
   0 & 0 & 1 & 0
  \end{matrix} 
\right]</script><script type="math/tex; mode=display">P
\left[
 \begin{matrix}
   x\\
   y\\
   z\\
   1\\
  \end{matrix} 
\right]=
\left[
 \begin{matrix}
   x\\
   y\\
   z\frac{n+f}{n}-f\\
   \frac{z}{n}\\
  \end{matrix} 
\right]=
\left[
 \begin{matrix}
   \frac{nx}{z}\\
   \frac{ny}{z}\\
   n+f-\frac{fn}{z}\\
   1\\
  \end{matrix} 
\right]</script><h2 id="Conclusion"><a href="#Conclusion" class="headerlink" title="Conclusion"></a>Conclusion</h2><p>将以上几种变换的矩阵合起来，可以得到将一个点$(x_{world}, y_{world}, z_{world})$映射到屏幕上$(x_{pixel}, y_{pixel}, z)$完整变换。首先是camera，世界坐标转换到相机坐标。之后是perspective，将确定的view volume转换成正投影的，然后将正投影空间用Orthography变换到canonical空间。最后是viewport，将canonical对应到像素上。Perspective和Orthography合并为Pespective矩阵。</p>
<script type="math/tex; mode=display">M_{per} = M_{orth}P</script><script type="math/tex; mode=display">M = M_{vp}M_{per}M_{cam}</script><script type="math/tex; mode=display">\left[
 \begin{matrix}
   x_{pixel}\\
   y_{pixel}\\
   z\\
   1\\
  \end{matrix} 
\right]=M
\left[
 \begin{matrix}
   x_{world}\\
   y_{world}\\
   z_{world}\\
   1\\
  \end{matrix} 
\right]</script>
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/CG/" rel="tag"># CG</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/09/06/树状数组、分桶法、线段树/" rel="next" title="树状数组、分桶法、线段树">
                <i class="fa fa-chevron-left"></i> 树状数组、分桶法、线段树
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/09/11/Point-Set-Registration-Coherent-Point-Drift/" rel="prev" title="Point Set Registration: Coherent Point Drift">
                Point Set Registration: Coherent Point Drift <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
      <div id="sidebar-dimmer"></div>
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/MOB.jpg" alt="HTelepathH">
            
              <p class="site-author-name" itemprop="name">HTelepathH</p>
              <p class="site-description motion-element" itemprop="description">Learning by doing.</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">25</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">12</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">15</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="99088577@qq.com" target="_blank" title="Mail">
                      Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://gitee.com/htelepathh" target="_blank" title="Gitee">
                      Gitee</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/htlpt" target="_blank" title="GitHub">
                      GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://weibo.com/u/6207125886" target="_blank" title="微博">
                      微博</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.zhihu.com/people/huang-he-tu" target="_blank" title="知乎">
                      知乎</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Affine-Transformation"><span class="nav-number">1.</span> <span class="nav-text">Affine Transformation</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Viewport-Transformation-windowing"><span class="nav-number">1.1.</span> <span class="nav-text">Viewport Transformation(windowing)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#The-Orthographic-Projection-Transformation"><span class="nav-number">1.2.</span> <span class="nav-text">The Orthographic Projection Transformation</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Camera-Transformation"><span class="nav-number">1.3.</span> <span class="nav-text">Camera Transformation</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Homography-Transformation"><span class="nav-number">2.</span> <span class="nav-text">Homography Transformation</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Perspective-Transformation"><span class="nav-number">2.1.</span> <span class="nav-text">Perspective Transformation</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Conclusion"><span class="nav-number">3.</span> <span class="nav-text">Conclusion</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-angle-double-left"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">HTelepathH</span>

  
</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          displayMath: [ ['$$', '$$']],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

</body>
</html>
